/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package front_end.tree;

import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import tsb.xmlparser.Node;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class CustomIconRenderer extends DefaultTreeCellRenderer {

    ImageIcon rootIcon, nodeIcon, nodeSelIcon, attributeIcon, attributeSelIcon;

    public CustomIconRenderer() {
        rootIcon = new ImageIcon(CustomIconRenderer.class.getResource("/images/root.png"));
        nodeIcon = new ImageIcon(CustomIconRenderer.class.getResource("/images/node.png"));
        nodeSelIcon = new ImageIcon(CustomIconRenderer.class.getResource("/images/node_selected.png"));
        attributeIcon = new ImageIcon(CustomIconRenderer.class.getResource("/images/attribute.png"));
        attributeSelIcon = new ImageIcon(CustomIconRenderer.class.getResource("/images/attribute_selected.png"));
    }

    public Component getTreeCellRendererComponent(JTree tree,
            Object value, boolean sel, boolean expanded, boolean leaf,
            int row, boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, sel,
                expanded, leaf, row, hasFocus);

        try{
            Node nodeObj = (Node)(((DefaultMutableTreeNode) value).getUserObject());
            // check whatever you need to on the node user object
            String text = nodeObj.getName();
            if (nodeObj.getValue() != null)
                text += ": \""+nodeObj.getValue()+"\"";
            setText(text);
            if (nodeObj.getProperties() != null)
                setToolTipText("Attributes: " + nodeObj.getProperties().size());
        } catch (ClassCastException e){}

        if(row == 0) setIcon(rootIcon);
        else if (!leaf) {
            setIcon(nodeIcon);
            if (sel) setIcon(nodeSelIcon);
        }
        else {
            setIcon(attributeIcon);
            if (sel) setIcon(attributeSelIcon);
        }
        

        return this;
    }
}
